// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from 'element-ui/packages/cascader-panel/src/store'
//import store from './store'

//设置反向代理，前端请求默认发送到http://localhost:8443/api
var axios = require('axios')
axios.defaults.baseURL = 'http://localhost:8443/api'
// 全局注册，之后可在其他组件中通过 this.$axios 发送数据
Vue.prototype.$axios = axios

Vue.config.productionTip = false

Vue.use(ElementUI)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  render: h => h(App),
  router,
  store, //引用store
  components: { App },
  template: '<App/>'
})

//在访问每一个路由前 调用
router.beforeEach((to, from, next) => {
  if(to.meta.requireAuth){ //判断要访问的页面是否需要登录
    if(store.state.user.username){ //如果需要登录，store里有没有user这个对象的信息
      next() //如果有，就放行，让它去执行后面的活
    }else{
      next({  //如果store里没有user这个对象的信息
        path: 'login', //就跳转到登录页面
        query: {redirect: to.fullPath}  //并存储访问的页面路径，以便在登录后直接跳转到相应网页
      })
    }
  }else {
    next()
  }
}
)

